/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.FlexEvent;

	public class EDIListGrid extends Canvas  implements IEDIDataLoad
	{
		private var _dataProvider:XML;
		private var _url:String;
		private var _parameter:String;
		private var _labelField:String;
		private var _tableName:String="";
		private var _img:String="";
		private var xList:XMLList;
		
		private const LOAD:String="DB_LOAD";
		private const LABELCHANGE:String="LABEL_CHANGE";
		private const DBCHANGE:String="DB_CHANGE";
		private var _comp:Boolean=false;
		private var _arrLab:Array;
		private var _arrImg:Array;
		private var _labX:Number=0;
		
		public function EDIListGrid()
		{
			super();
			_arrLab=new Array();
			_arrImg=new Array();
		}
		
		
		public function set dataProvider(value:XML):void
		{
			if(tableName==""){tableName=value["@name"].toString()}
			_dataProvider=value;
			xList=_dataProvider.children();
			createList();
		}
		
		public function get dataProvider():XML
		{
			return _dataProvider;
		}
		
		public function set url(value:String):void{_url=value;}
		public function get url():String{return _url;}
		
		public function set img(value:String):void
		{
			_img=value;
			labX=value==""?0:20;
		}
		public function get img():String{return _img;}
		
		protected function set labX(value:Number):void
		{
			_labX=value;
			var i:int;
			for(i=0;i<_arrLab.length;i++)
			{
				(_arrLab[i] as Label).x=value;
			}
		}
		protected function get labX():Number{return _labX;}
		
		//id=@id&page=!age&edit=1 参数方式 @id 代表 取URL里的 id !age 代表取数据库内的age值
		public function set parameter(value:String):void{_parameter=value;}
		public function get parameter():String{return _parameter;}	
		
		public function set labelField(value:String):void
		{_labelField=value;}
		public function get labelField():String{return _labelField;}	
		
		public function set tableName(value:String):void{
			_tableName=value;
			EDIEventProxy.listenerDataLoad(value,this);
		}
		public function get tableName():String{return _tableName;}		
		
		
		
		private function createLabel():Label
		{
			var _lab:Label=new Label();
			_lab=new Label();
			_lab.addEventListener(MouseEvent.MOUSE_OUT,function(event:MouseEvent):void{
					(event.currentTarget as Label).setStyle("textDecoration","none");
				}
			);
			_lab.addEventListener(MouseEvent.MOUSE_OVER,function(event:MouseEvent):void{
					(event.currentTarget as Label).setStyle("textDecoration","underline");
				}
			);
			_lab.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				var paraArr:Array=parameter.split('|');
				var i:int;
				var para:String="";
				var _lab:Label=event.currentTarget as Label;
				var pa:Array;
				var sPara:String;
				for(i=0;i<paraArr.length;i++)
				{
					pa=paraArr[i].toString().split('=');
					if(pa.length!=0)
					{
						sPara=pa[1].toString();
						switch(pa[1].toString().charAt(0))
						{
							case "@":
							{
								sPara=pa[1].toString().substring(1,pa[1].toString().length);
								sPara=parentApplication.parameters[sPara];											
								break;
							}	
							case "#":
							{
								sPara=pa[1].toString().substring(1,pa[1].toString().length);
								sPara=xList[_lab.data["index"]][sPara];
								break;
							}
						}
						para+=(para==""?"":"&")+pa[0]+"="+sPara;
					}
				}
				var url1:URLRequest=new URLRequest(this.url+"?"+para);
				flash.net.navigateToURL(url1,"_self");  
				}
			);
			//_lab.width=width-labX;
			_lab.height=20;
			return _lab;
		}
		
		//
		private function createList():void
		{
			if(dataProvider.children()!=null && labelField!="" && _comp)
			{
				var i:int;
				var _lab:Label;
				var _img:Image;
				var obj:Object;
				
				this.removeAllChildren();
				for(i=0;i<xList.length();i++)
				{
					if(img!="")
					{
						_img=new Image();
						_img.source=img.charAt(0)!="!"?img:xList[i][img.substr(1)];
						_img.width=20;
						_img.height=20;
						_img.y=i*20;
						_img.x=0;
						addChild(_img);
						_arrImg.push(_img);
					}
					_lab=createLabel();
					obj=new Object();
					obj["index"]=i;
					_lab.data=obj;
					_lab.htmlText="<A href='#'>"+xList[i][labelField]+"</A>"
							
					addChild(_lab);
							
					_lab.x=labX;
					_lab.y=i*20;
					_arrLab.push(_lab);
				}
				
				/*
				switch(event)
				{
					case LOAD:
					{				
						for(i=0;i<xList.length();i++)
						{
							if(img!="")
							{
								_img=new Image();
								_img.source=img.charAt(0)!="!"?img:xList[i][img.substr(1)];
								_img.width=20;
								_img.height=20;
								_img.y=i*20;
								_img.x=0;
								addChild(_img);
								_arrImg.push(_img);
							}
							_lab=createLabel();
							obj=new Object();
							obj["index"]=i;
							_lab.data=obj;
							_lab.htmlText="<A href='#'>"+xList[i][labelField]+"</A>"
							
							
							addChild(_lab);
							
							_lab.x=labX;
							_lab.y=i*20;
							_arrLab.push(_lab);
						}
						break;
					}
					case DBCHANGE:
					{
						var len:int=xList.length()-_arrLab.length;
						if(len>0)
						{
							for(i=_arrLab.length;i<xList.length();i++)
							{
								if(img!="")
								{
									_img=new Image();
									_img.source=img.charAt(0)!="!"?img:xList[i][img.substr(1)];
									_img.width=20;
									_img.height=20;
									_img.y=i*20;
									_img.x=0;
									addChild(_img);
									_arrImg.push(_img);
								}
								_lab=createLabel();
								obj=new Object();
								obj["index"]=i;
								_lab.data=obj;								
								_lab.x=labX;
								_lab.y=i*20;
								addChild(_lab);
								_arrLab.push(_lab);
							}
						}
						else if(len<0)
						{
							for(i=_arrLab.length-1;i>=xList.length();i--)
							{
								this.removeChild(_arrLab[i] as Label);
								this.removeChild(_arrImg[i] as Image);
							}
							_arrLab.splice(xList.length(),Math.abs(len));
							_arrImg.splice(xList.length(),Math.abs(len));
						}
						createList(LABELCHANGE);
						break;
					}
					case LABELCHANGE:
					{
						for(i=0;i<_arrLab.length;i++)
						{
							(_arrLab[i] as Label).htmlText="<A href='#'>"+xList[i][labelField]+"</A>";
						}
						break;
					}
				}
				*/
			}
		}
		
		public function dataLoadHandler(event:EDIDataEvent):void
		{
			dataProvider=event.value as XML;
		}
		
		private function creationComplete(event:FlexEvent):void
		{
			_comp=true;
		}		
		
	}
}